/**
 * AbstractController class links both model and view
 * 
 **/
package javaapplication4.controller;

import java.util.ArrayList;
import javaapplication4.model.*;
import javaapplication4.view.ContactForm;
import javaapplication4.view.ListAlphaView;
import javax.swing.JTextField;

/**
 * Abstract Controller to allow polymorphism
 * @author mathieubakrim
 **/
public abstract class AbstractController {
    protected AbstractModel adressBook;
    protected ArrayList<Contact> contactsList = new ArrayList<Contact>();

    /**
     * Getter
     * @return List of contacts actually saved in memory
     **/
    public ArrayList<Contact> getContactsList() {
        return contactsList;
    }

    /*Controller need to interact with the model, so it has to possess a model instance
     * @param in : model instance
     */
    public AbstractController(AbstractModel adr){
        this.adressBook = adr;
    }
    
    /*
     * Control datas & inform the model
     * Not implemented yet
     */
    abstract void control();

    /**
     * Create a contact with datas read from the view
     * @param in: array of the Createform's textfields
     * @param out: an Object Contact, saved in controller arraylist
     **/
    public void createContact(JTextField[] tab_textField) {
       String ln = tab_textField[0].getText();
       String fn = tab_textField[1].getText();
       String pn = tab_textField[2].getText();
       String ap = tab_textField[3].getText();
       String ape = tab_textField[4].getText();
       String hp = tab_textField[5].getText();
       String ep = tab_textField[6].getText();
       String epe = tab_textField[7].getText();
       
       ln = ln.toUpperCase(); // Save all names in uppercase
       
       contactsList.add(this.adressBook.createContact(ln, fn, pn, ap, ape, hp, ep, epe));
       
       /*
        * Simple Test to track Contact creation
        * To delete at the end
       */
        System.out.println("Test de creation de contact dans AdressBook.java :");
        System.out.println(contactsList.get(0).toString());
    }
        
    /**
     * Show all contacts, alphabeticallly
     * @param out: instantiate a new view to show the list
     **/
    public void showAlphaList(){              
        ListAlphaView alphaList = new ListAlphaView(this);       
    }

    /**
     * Show the contact form view
     * @param out: a new view to create a contact
     **/
    public void showContactForm() {
       ContactForm contactForm = new ContactForm(this);
    }
  
    /**
     * Delete a contact
     * @Todo : destroy the object after removing it from the list
     **/
    public void deleteContact(Contact c){
        contactsList.remove(c);
    }
    
    /**
     * edit a contact
     * @param in: old contact, new contact
     * @param out: new contact
     **/
    public void editContact(Contact cOld, Contact cNew){
        contactsList.remove(cOld);
        contactsList.add(cNew);
    }
    
    /**
     * Show contact details
     * @param c : a contact
     * @return a contact to String
     **/
    public String showDetails(Contact c){
        return c.toString();
    }
}
